const express = require('express');
const app = express()
const axios = require('axios');

app.get("/register", (req, res) => {
    res.send({ code: 0, data: "哈哈哈哈哈" })
})


// 响应头 Content-disposition 是Content-Disposition: attachment; filename="index.js"  标明下载文件
app.get("/download", (req, res) => {
    res.download("./index.js")
})
// 就没有了 Content-Disposition:  属性
app.get("/see", (req, res) => {
    res.sendFile(__dirname + "/index.js")
})
// Content-Type: application/octet-stream
// 
app.get("/test", (req, res) => {
    axios.get("http://localhost:4001/download", {responseType: "arraybuffer"}).then((result) => {
    // axios.get("http://localhost:4001/download", {}).then((result) => {
        console.log(result.date);
        res.type("pdf")  //如果指定了type 那么就能在线预览
        res.send(result.data)
    }).catch((err) => {
        console.log(err);
        res.send("err")
    });
})

app.get("/testword", (req, res) => {
    axios.get("http://localhost:4001/downloadword", {responseType: "arraybuffer"}).then((result) => {
    // axios.get("http://localhost:4001/download", {}).then((result) => {
        console.log(result.date);
        res.setHeader("Content-Disposition"," attachment; filename=index.docx")
        res.send(result.data)
    }).catch((err) => {
        console.log(err);
        res.send("err")
    });
})

app.listen(4000, () => {
    console.log("listen");
})